using System;
using HMCollada.Core;

namespace HMCollada.Core {
    internal struct HMColladaValueArray {
        private int count;
        private string id;
        private string name;
        private object[] array;

        public int Count { get { return count; } set { count = value; } }
        public string Id { get { return id; } set { id = value; } }
        public string Name { get { return name; } set { name = value; } }
        public object[] Array { get { return array; } set { array = value; } }
    }
}

namespace HMCollada {
    internal partial class HMColladaLoader {
        private enum Types {
            String,
            Bool,
            Float,
            Int,
            None
        }
        private static Types EnumerateType(Type t) {
            if (t == typeof(string)) return Types.String;
            if (t == typeof(bool)) return Types.Bool;
            if (t == typeof(float)) return Types.Float;
            if (t == typeof(int)) return Types.Int;

            return Types.None;
        }

        private static HMColladaValueArray LoadValueArray(Type t) {
            HMColladaValueArray newArray = new HMColladaValueArray();
            newArray.Id = colladaReader.GetAttribute("id");
            newArray.Name = colladaReader.GetAttribute("name");
            newArray.Count = int.Parse(colladaReader.GetAttribute("count"));
            newArray.Array = new object[newArray.Count];

            string content = ReadString().Trim();
            content = content.Replace('\r', ' ').Replace('\n' , ' ');
            while (content.Contains("  ")) {
                content = content.Replace("  ", " ");
            }

            string[] values = content.Split(new char[] { ' ' }, newArray.Count);
            switch (EnumerateType(t)) {
                case Types.String: newArray.Array = values; break;
                case Types.Bool: for (int i = 0; i < values.Length; i++) newArray.Array[i] = bool.Parse(values[i]); break;
                case Types.Float: for (int i = 0; i < values.Length; i++) newArray.Array[i] = float.Parse(values[i]); break;
                case Types.Int: for (int i = 0; i < values.Length; i++) newArray.Array[i] = int.Parse(values[i]); break;
            }

            if (colladaReader.IsEmptyElement) colladaReader.Read();
            return newArray;
        }
    }
}